define(["SzLib", "SzSns", "jquery", "jquery.json"], function(SzLib, SzSns) {

    /**
     * Facebook implementation of SzSns.
     */
    var SzSnsVk = function() {
        // Call the parent constructor
        SzSns.call(this);
    };
    // inherit SzSns
    SzSnsVk.prototype = new SzSns();
    // correct the constructor pointer because it points to SzSns
    SzSnsVk.prototype.constructor = SzSnsVk;


    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    //-* API Function Overwrites
    //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    /**
     * 获取玩家信息
     *
     * @see SzSns.prototype.getUserInfo
     */
    SzSnsVk.prototype.getUserInfo = function(platformId, callback) {
        return VK.api("users.get", {
            uids:platformId,
            fields:"uid, first_name, last_name, photo_max"
        }, function(r) {
            response = r.response[0];
            var userInfo = {
                id: response.uid,
                name: response.first_name + ' ' + response.last_name,
                gender: '',
                locale: '',
                profile_image_url: response.photo_max,
                timezone: '',
                email: '',
                first_name: response.first_name,
                last_name: response.last_name
            };
            SZ.APP.callAsFunc("setUserInfo", userInfo);
            if (typeof callback == 'function') {
                callback(userInfo);
            }
            return userInfo;
        });
    };

    /**
     * 获取复数玩家信息
     *
     * @see SzSns.prototype.getUserInfos
     */
    SzSnsVk.prototype.getUserInfos = function(platformIds, callback) {
        return VK.api("users.get", {
            uids:platformIds,
            fields:"uid, first_name, last_name, photo_max"
        }, function(r) {
            var userInfos = {};
            if (typeof r === 'object') {
                $.each(r.response, function(key, friendInfo) {
                    platformId = friendInfo.uid;
                    if (platformId != SZ.PLATFORM_ID) {
                        userInfos[platformId] = {
                            id: platformId,
                            name: friendInfo.first_name  + ' ' + friendInfo.last_name,
                            profile_image_url: friendInfo.photo_max
                        };
                    }
                });
            }
            SZ.APP.callAsFunc("setUserInfos", userInfos);
            if (typeof callback == 'function') {
                callback(userInfos);
            }
            return userInfos;
        });
    };

    /**
     * 获取好友列表信息
     *
     * @see SzSns.prototype.getFriends
     */
    SzSnsVk.prototype.getFriends = function(platformId, callback) {
        return VK.api("friends.get", {
            user_id:platformId,
            count:300,
            offset:0,
            fields:"uid, first_name, last_name, photo_max"
        }, function(r) {
            var userInfos = {};
            if (typeof r === 'object') {
                $.each(r.response, function(key, friendInfo) {
                    if (friendInfo.uid != SZ.PLATFORM_ID) {
                        userInfos[friendInfo.uid] = {
                            id: friendInfo.uid,
                            name: friendInfo.first_name + ' ' + friendInfo.last_name,
                            profile_image_url: friendInfo.photo_max
                        };
                    }
                });
            }
            SZ.APP.callAsFunc("setFriendList", userInfos);
            if (typeof callback == 'function') {
                callback(userInfos);
            }
            return userInfos;
        });
    };

    /**
     * 获取玩家access token的接口
     *
     * @see SzSns.prototype.getAccessToken
     */
    SzSnsVk.prototype.getAccessToken = function() {
        return null;
    };

    /**
     * 获取玩家本地化支付，现金单位与汇率的接口
     *
     * @see SzSns.prototype.getCurrency
     */
    SzSnsVk.prototype.getCurrency = function(params, callback) {
        var r = {
            currency : {
                usd_exchange_inverse: 6, // 美元兑卢布，固定1:65  美元兑 votes 1:6
                user_currency: 'Голосов'
            }
        };
        SZ.APP.callAsFunc("setCurrency", r);
        callback(params, r);
    };

    /**
     * 服务器生成好feedId和link后，前端调用sendFeed接口，当真正发布feed后，在回调函数中调用as接口，
     * 前端记录已发送feed的类型，并调用后端接口，将feedId传给服务器，服务器更新feedId对应的状态
     *
     * @see SzSns.prototype.sendFeed
     * @param feedId
     * @param feedDefId
     * @param feedInfo
     * @param platformId
     * <pre>
     *    ｛
     *          name:[param, param, ...],
     *          caption:[param, param, ...],
     *          description:[param, param, ...],
     *          picture:图片在社区照片墙上的id,
     *     ｝
     * </pre>
     */
    SzSnsVk.prototype.sendFeed = function(feedId, feedDefId, feedInfo, platformId) {
        // ad_eyJmZWVkSWQiOjEyMywicGxhdGZvcm1JZCI6NDU2fQ==
        // var feedUrl = SZ.CANVAS_URL + '?feedId=' + feedId + '&platformId=' + platformId + '&act_from=feed&fb_source=feed';
        var feedStr = SzLib.Base64.encode((JSON.stringify({
            "feedId": feedId,
            "platformId": platformId
        })));
        var feedUrl = SZ.CANVAS_URL + '?ad_id=' + feedStr;


        $("#gameSwf").addClass("hideSwf");
        $("#gameImage")
            .html('<img src="data:image/jpeg;base64,' + SZ.APP.callAsFunc("getStageImage", []) + '" width="100%">')
            .show();

        var obj = {
            owner_id:platformId,
            from_group:1,
            message:SzLib.strReplace(SZ.FEED_JSON[feedDefId].description, feedInfo.description), // feed description 新鲜事内容描述,
            attachments:"photo-" + SZ.FEED_JSON[feedDefId].picture + ", " + feedUrl
        };

        VK.api("wall.post", obj, function(data) {
            //callBack function
            $("#gameSwf").removeClass("hideSwf");
            $("#gameImage").html("").hide();
            SZ.APP.callAsFunc("sendFeedCallBack", [data]);
            SzLib.log(data);
        });
    };

    /**
     * 前端调用sendRequest接口，当真正发送request后，在回调函数中调用as接口，
     * 前端记录已发送request的类型和已发送的userId，并调用后端接口，
     * 后端根据userId创建request数据
     *
     * @see SzSns.prototype.sendRequest
     * @param userIds
     * @param requestDefId
     * @param requestInfo
     * <pre>
     *    ｛
     *          title:[param, param, ...],
     *          message:[param, param, ...],
     *     ｝
     * </pre>
     */
    SzSnsVk.prototype.sendRequest = function(userIds, requestDefId, requestInfo) {
        $("#gameSwf").addClass("hideSwf");
        $("#gameImage")
            .html('<img src="data:image/jpeg;base64,' + SZ.APP.callAsFunc("getStageImage", []) + '" width="100%">')
            .show();

        $.each(userIds, function(key, userId) {
            SzLib.log(userId);
            VK.callMethod("showRequestBox", parseInt(userId), SzLib.strReplace(SZ.REQUEST_JSON[requestDefId].message, requestInfo.message));
            VK.addCallback("onRequestSuccess", function(res) {
                SzLib.log(res);
                $("#gameSwf").removeClass("hideSwf");
                $("#gameImage").html("").hide();
                SZ.APP.callAsFunc("sendRequestCallBack", [res.request]);
            });
            VK.addCallback("onRequestFail", function(res) {
                SzLib.log(res);
                $("#gameSwf").removeClass("hideSwf");
                $("#gameImage").html("").hide();
                SZ.APP.callAsFunc("sendRequestCallBack", ['false']);
            });
            VK.addCallback("onRequestCancel", function(res) {
                SzLib.log(res);
                $("#gameSwf").removeClass("hideSwf");
                $("#gameImage").html("").hide();
                SZ.APP.callAsFunc("sendRequestCallBack", ['false']);
            });
        });
    };

    /**
     * 发送sendOpenGraph接口，
     *
     * @see SzSns.prototype.sendOpenGraph
     * @param actionType // e.g: harvest
     * @param objectType // e.g: flower
     * @param graphInfo // json string e.g: [[title,...], [description,...]], 具体需要参考openGraph的文档进行设置，默认为空“[[],[]]”
     */
    SzSnsVk.prototype.sendOpenGraph = function(actionType, objectType, graphInfo) {
        // 无
    };

    /**
     * @see SzSns.prototype.orderPayment
     */
    SzSnsVk.prototype.orderPayment = function(paymentDefId, extra, type) {
        var addParams = {
            "token": SZ.TOKEN,
            "dev": SzLib.getUrlParam('dev')
        };
        var params = {
            type: 'item',
            item: paymentDefId
        };
        if ($("#gameShadow").is(":hidden")) {
            $("#gameSwf").addClass("hideSwf");
            $("#gameImage")
                .html('<img src="data:image/jpeg;base64,' + SZ.APP.callAsFunc("getStageImage", []) + '" width="100%">')
                .show();
        }
        VK.callMethod("showOrderBox", params);
        VK.addCallback('onOrderSuccess', function(res) {
            if ($("#gameShadow").is(":hidden")) {
                $("#gameSwf").removeClass("hideSwf");
                $("#gameImage").html("").hide();
            }

            SzLib.sendSzReq(
                ["page.index", []],
                function(r) {
                    SzLib.log(r);
                    SZ.APP.paymentSucceed(r);
                },
                function(r) {
                    SzLib.log(r);
                },
                $.extend(addParams, {
                    "act": "complete",
                    "res": res,
                    "auth_key": SZ.PF_TOKEN,
                    "viewer_id": SZ.PLATFORM_ID,
                    "token": SZ.TOKEN
                }),
                SZ.IS_COMPRESS
            );
        });
        VK.addCallback('onOrderFail', function(r) {
            if ($("#gameShadow").is(":hidden")) {
                $("#gameSwf").removeClass("hideSwf");
                $("#gameImage").html("").hide();
            }
        });
        VK.addCallback('onOrderCancel', function(r) {
            if ($("#gameShadow").is(":hidden")) {
                $("#gameSwf").removeClass("hideSwf");
                $("#gameImage").html("").hide();
            }
        });
    };

    /**
     * 打开邀请好友界面
     *
     * @returns void
     */
    SzSnsVk.prototype.inviteBox = function() {
        SzLib.log('sina invite box');
        SZ.APP.callAsFunc("closeJsPanel", []);
        SZ.APP.callAsFunc("openInviteNeighborsPanel", []);
    };

    return new SzSnsVk();

});